﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCD.UI
{
    public partial class ZMCD07 : EFViewCmes
    {
        private string CurrentDir;
        private List<EIRow> Rows;

        public ZMCD07(string currentDir, List<EIRow> rows)
        {
            this.CurrentDir = currentDir;
            this.Rows = rows;
            InitializeComponent();
            this.tree.Refresh();
            //this.Loaded += new RoutedEventHandler(ZMCD07_Loaded);
        }

        void ZMCD07_Loaded(object sender, RoutedEventArgs e)
        {
            this.tree.Refresh();
        }

        /// <summary>
        /// 页面初始化的时候，选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;

            if (treeItem.Level == 0 && treeItem.Index == 0)
            {
                treeItem.ExpandItem();//展开
            }
            string id = treeItem.ItemObject.GetType().GetProperty("label").GetValue(treeItem.ItemObject, null) as string;
            if (CurrentDir.Equals(id))
            {
                this.tree.SelectedItem = treeItem;//选中
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.tree.SelectedItems.Count>0)
            {
                EIRow item= (((this.tree.SelectedItems[0] as EFTreeItem).ItemObject) as UIBaseEntity).GetEIRow();
                string id = item["label"].ToString();
                string limit = item["limit"].ToString();

                if ((this.tree.SelectedItems[0] as EFTreeItem).IsRootItem)
                {
                    this.ValidateMessagePopup("不能将文档迁移到根目录下，请重新选择！");
                    this.TempButtonOpen(true);
                    return;
                }

                if (!"1".Equals(limit))
                {
                    this.ValidateMessagePopup("您没有该目录下的编辑权限，不能将文档迁移到该目录下！");
                    this.TempButtonOpen(true);
                    return;
                }

                if (CurrentDir.Equals(id))
                {
                    this.ValidateMessagePopup("该文档已经在该目录下，请重新选择！");
                    this.TempButtonOpen(true);
                    return;
                }

                EIInfo _info = new EIInfo();
                _info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCD02");
                _info.Attributes.Add(EIConstants.MethodNameKey, "move");

                EIBlock Block = _info.AddBlock(EIConstants.ResultBlock);
                EIColumn dir = new EIColumn();
                dir.Name = "fdDirId";
                dir.TypeLabel = "C";
                Block.AddColumn(dir);

                dir = new EIColumn();
                dir.Name = "fdKeyId";
                dir.TypeLabel = "C";
                Block.AddColumn(dir);

                foreach (EIRow row in Rows)
                {
                    row["fdDirId"] = id;
                }
                Block.SetRows(Rows);

                this.Submit(_info, MoveCompleted);  
                
            }
        }

        /// <summary>
        /// 迁移的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.ValidateMessagePopup("操作失败！");//操作失败，消息提醒
            }
            else
            {
                //操作成功关闭新增页面
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            EFWindow _window = this.Parent as EFWindow;
            _window.DialogResult = false;
            _window.Close();
        }


        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
